/**
 * 功能：处理日程安排中的客户端操作
 * 作者：zyxun
 * 日期：2012/8/30
 */

var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
 


//页面初始化事件
$(document).ready(function(){
	
	var op = {
		theme: true,
	    defaultView: 'month',
	    contentHeight: 470,
	    header: {
					left: 'prev,next today',
					center: 'title',
					right: 'month,agendaWeek,agendaDay'
				},
	    titleFormat: { month: 'yyyy' +str_year+ 'MMMM',
                   week: "MMMMd"+str_day+"{'&nbsp;&nbsp;"+str_to+"&nbsp;&nbsp;'[yyyy"+str_year+"][MMMM]d"+str_day+"}",
                   day: 'yyyy-MM-d ddd'

                },
	    allDayText: str_allday,
	    axisFormat:'HH:mm',
	    timeFormat:'HH:mm{ - HH:mm}',
	    buttonText:{
			prev:     str_yesterday,
			next:     str_tomorrow,
			prevYear: str_lastYear,
			nextYear: str_nextYear,
			today:    str_today,
			month:    str_month,
			week:     str_week,
			day:      str_day
		},
	    monthNames: [str_january, str_february, str_march, str_april, str_may, str_june, str_july, str_august, str_september, str_october, str_november, str_december],
	    monthNamesShort: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
	    dayNamesShort: [str_sunday, str_monday, str_tuesday, str_wednesday, str_thursday, str_friday, str_saturday],
	    selectable: true,
	    selectHelper: true,
	    select: function(start, end, allDay) {
					showNewEventWindow($.fullCalendar.formatDate(start,'yyyy-MM-dd HH:mm'),
					 	$.fullCalendar.formatDate(end,'yyyy-MM-dd HH:mm'), allDay);
					$('#calendar').fullCalendar('unselect');
				},
	    editable: true,
	    diableResizing: false,
		eventClick:function(calEvent,jsEvent,view) { 
			showmodifyEventWindow(calEvent.id);
		},
		viewDisplay:function(view){
			showEventsToCalendar($.fullCalendar.formatDate(view.start,'yyyy-MM-dd HH:mm'),
					 	$.fullCalendar.formatDate(view.end,'yyyy-MM-dd HH:mm'));
		},	
		eventDrop:function( calEvent, dayDelta, minuteDelta, allDay, revertFunc, jsEvent, ui, view ){
			saveEventDropTime(calEvent.id,dayDelta,minuteDelta,allDay,false,revertFunc);
		},
		eventResize: function(calEvent, dayDelta, minuteDelta, revertFunc, jsEvent, ui, view){
			saveEventDropTime(calEvent.id,dayDelta,minuteDelta,false,true,revertFunc);
		}
	}

	var calendar = $('#calendar').fullCalendar(op);
});

//打开新建日程事件窗口
function showNewEventWindow(start, end, allDay){
	var url = '/dailyplan/maintanceEvensInfo.action?start=' + start;
	url += '&end=' + end;
	url += '&allDay=' + allDay;
	art.dialog.open(url, {
	    title:str_MaintanceEventTitle,
        width: '700px',
        height: '280px',
        lock: true,
        ok: false,
	    button: [
	        {
	            name: str_save,
	            callback: function () {
	                var iframe = this.iframe.contentWindow;
			     	if (!iframe.document.body) {
			         	art.dialog(str_onloding);
			         	return false;
			         };
			     	iframe.doSave($('#calendar'));
			     	return false;
	            },
	            focus: true
	        },
	        {
	            name: str_close
	        }
	    ]
	});
}

//打开已经存在的日程事件详细信息窗口
function showmodifyEventWindow(eventId){
	url = '/dailyplan/maintanceEvensInfo.action?eventId=' + eventId;
	art.dialog.open(url, {
	    title:str_MaintanceEventTitle,
        width: '700px',
        height: '280px',
        lock: true,
        ok: false,
	    button: [
	        {
	            name: str_save,
	            callback: function () {
	                var iframe = this.iframe.contentWindow;
			     	if (!iframe.document.body) {
			         	art.dialog(str_onloding);
			         	return false;
			         };
			     	iframe.doSave($('#calendar'));
			     	return false;
	            },
	            focus: true
	        },
	        {
	            name: str_delete,
	            callback: function () {
	                deleteEvent(eventId);
	            }
	        },
	        {
	            name: str_close
	        }
	    ]
	});
}

function newShowEventWindow(url,eventId){
	art.dialog.open(url, {
	    title:str_MaintanceEventTitle,
        width: '700px',
        height: '280px',
        lock: true,
	    button: [
	        {
	            name: str_save,
	            callback: function () {
	                var iframe = this.iframe.contentWindow;
			     	if (!iframe.document.body) {
			         	art.dialog(str_onloding);
			         	return false;
			         };
			     	iframe.doSave($('#calendar'));
			     	return false;
	            },
	            focus: true
	        },
	        {
	            name: str_delete,
	            callback: function () {
	                deleteEvent(eventId);
	            }
	        },
	        {
	            name: str_close
	        }
	    ]
	});

}

//打开维护日程事件的窗口
function showEventWindow(url){
    art.dialog.open(url, {
        title:str_MaintanceEventTitle,
        width: '700px',
        height: '280px',
        lock: true,
        ok: function () {
	     	var iframe = this.iframe.contentWindow;
	     	if (!iframe.document.body) {
	         	art.dialog(str_onloding);
	         	return false;
	         };
	     	iframe.doSave($('#calendar'));
	     	return false;
	     },
	     cancel: true,
	     cancelVal: str_close,
         okVal: str_save   
    })
}

//以异步的方式删除指定的事件
function deleteEvent(eventId){
	art.dialog.confirm(str_delete_comfirm, function(){
		    $.ajax({
			type:"post",
			url:"/dailyplan/deleteEventAJAX.action",
			data:{"eventId":eventId},
			datatype:"json",
			success:function(res){
				var obj = $.parseJSON(res);
				if(!obj.result && obj.info.length>0){			
					art.dialog(obj.info);
				} else{
					$('#calendar').fullCalendar('removeEvents', eventId); 
				}
			}
		});
	}, function(){
		
	});
	
}

//异步方式向日历控件上显示已经有的事件
function showEventsToCalendar(start,end){
	//清除日历上的所有事件
	$('#calendar').fullCalendar('removeEvents'); 
	//异步获取指定日期内的事件
	$.ajax({
	  type: "post",
	  url: "/dailyplan/getEventsListAJAX.action",
	  data:{"startTime":start,"endTime":end},
	  dataType: "json",
	  success: function(jsonEvent){
	  	if(jsonEvent.eventTotal>0){
			$.each(jsonEvent.eventData,function(i,event){
				addEventToCalendar(event,true);
			});
		}
	  }
	});
}

//通过异步的方式向后提交拖拽后事件的时间数据进行保存
function saveEventDropTime(id,days,minutes,allDay,reSize,revertFunc){
	//异步保存
	$.ajax({
		type:"post",
		url: "/dailyplan/saveEventTimeAJAX.action",
		data: {"eventId":id,"days":days,"minutes":minutes,"allDay":allDay,"reSize":reSize},
		dataType: "json",
		success: function(obj){
			if(!obj.result){
				revertFunc();
			}
		}
	});
}

//添加一个日程事件的方法
function addEventToCalendar(calEvent,stick){
	 $('#calendar').fullCalendar('renderEvent', calEvent, stick); 
}
//更新一个日程事件方法
function updateEventToCalendar(calEvent){
	$('#calendar').fullCalendar('removeEvents', calEvent.id); 
	addEventToCalendar(calEvent,true);
}
